//
//  WMH_WSBS_ItemDetail_ViewController.m
//  EpointFrame5
//
//  Created by AndLi on 15/4/23.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//  网上办事 事项详情

#import "WMH_WSBS_ItemDetail_ViewController.h"

#import "RightButtonList.h"
#import "WSBSItemCateListModel.h"
#import "WSBSItemCell.h"
#import "WSBSInfoListModel.h"
#import "WMH_WSBS_InfoDetail_ViewController.h"

#define TaskId_GetWsbsList 1
#define TaskId_GetWsbsInfoList 2
#define TaskId_GetWsbsInfoDetail 3
@interface WMH_WSBS_ItemDetail_ViewController ()<RightButtonListDelegate,UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>
{
    NSString *CateName;
    NSString *CateGuid;
    
    NSMutableArray *menuNames;
    NSMutableArray *menuImgs;
    
    NSMutableArray *cateArray;
    
    NSString *listid;
    
    UITableView *mTableView;
    UIWebView *mWebView;
    
    NSMutableArray *infoListArray;
}
@property (nonatomic, retain) RightButtonList *btnList;
@end

@implementation WMH_WSBS_ItemDetail_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    menuNames = [[NSMutableArray alloc]init];
    menuNames = [[NSMutableArray alloc]init];
    
    [self setRightBarButtonItemWithImage:@"img_wmh_wsbs_menu.png" action:@selector(clickMenu)];
    
    CateName = [self.passParam objectForKey:@"CateName"];
    CateGuid = [self.passParam objectForKey:@"CateGuid"];
    
    //获取办理事项分类
    [self showMBPHUD];
    [self startTask:@"Task_GetWsbsList" withParams:nil withTaskId:TaskId_GetWsbsList];
    
    mTableView = [[UITableView alloc] initWithFrame:[self getFullContentFrame]];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    [self.view addSubview:mTableView];
    
    [mTableView setHidden:YES];
    
    mWebView = [[UIWebView alloc] initWithFrame:[self getFullContentFrame]];
    mWebView.delegate = self;
    [self.view addSubview:mWebView];
    
    [mWebView setHidden:YES];
    
    

}

-(void)clickMenu{
    [_btnList showAndHideButtonList];
}

#pragma mark ---------菜单点击事件---------
- (void)cellClicked:(NSString *)cellText atIndexPath:(NSIndexPath *)indexPath {
    [self getData:indexPath.row];
}

-(void)getData:(int)index{
    WSBSItemCateListModel *info = [cateArray objectAtIndex:index];
    NSLog(@"是否单条 %@",info.isSingleInfo);
    listid = info.ListID;
    
    //单条
    if([info.isSingleInfo isEqualToString:@"True"]){
        [mWebView setHidden:NO];
        [mTableView setHidden:YES];
        [self getWsbsInfoDetail:CateGuid withListId:listid];
    }
    //列表
    else{
        [mWebView setHidden:YES];
        [mTableView setHidden:NO];
        [self getWsbsInfoList:CateGuid withListId:listid];
    }

}

-(void)getWsbsInfoList:(NSString *)_categuid withListId:(NSString *)_listid{
    [self showMBPHUD];
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setObject:_categuid  forKey:@"CateGuid"];
    [param setObject:_listid    forKey:@"ListID"];
    [self startTask:@"Task_GetWsbsInfoList" withParams:param withTaskId:TaskId_GetWsbsInfoList];
}

-(void)getWsbsInfoDetail:(NSString *)_categuid withListId:(NSString *)_listid{
    [self showMBPHUD];
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setObject:_categuid  forKey:@"CateGuid"];
    [param setObject:_listid    forKey:@"ListID"];
    [self startTask:@"Task_GetWsbsSingleInfoList" withParams:param withTaskId:TaskId_GetWsbsInfoDetail];
}


-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    [self hideMBPHUD];
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        return;
    }
    
    if (taskId==TaskId_GetWsbsList) {
        
        cateArray = [arr objectAtIndex:2];
        NSLog(@"事项分类 %i",[cateArray count]);
        
        if ([cateArray count]>0) {
            for (int i=0; i<[cateArray count]; i++) {
                WSBSItemCateListModel *info = [cateArray objectAtIndex:i];
                [menuNames addObject:info.ListName];
                [menuImgs addObject:@"img_wmh_wsbs_menu.png"];
            }
        }
        self.btnList = [[RightButtonList alloc] initWithContentWidth:80];
        _btnList.tableDataArray = menuNames;
        _btnList.imageDataArray = menuImgs;
        _btnList.delegate = self;
        [self.view addSubview:_btnList];
        
        //默认获取第一项
        [self getData:0];
    }
    
    if (taskId == TaskId_GetWsbsInfoList) {
        infoListArray = [arr objectAtIndex:2];
        [mTableView reloadData];
    }
    
    if (taskId == TaskId_GetWsbsInfoDetail){
        NSString *result = [arr objectAtIndex:2];
        
        if([result rangeOfString:@"title"].location != NSNotFound){
            NSString *newscontent_fontsize = [DBFrameService getConfigValue:WMH_NewsContent_FontSize];
            //注意：css拼接中使用%%，进行百分号的格式化处理
            NSString * mycss =[NSString stringWithFormat:@"<style>p{margin:0px;padding:0px;}#news_outline {font-family:System;color: #5a5a5a; font-size:14px;text-align:left;padding-bottom:16px;padding-top:4px;}.ParagraphIndent img,p img{width:100%%;max-width:100%%;display:block;margin-bottom: 10px;}.ParagraphIndent,p,P{-webkit-box-sizing:border-box;box-sizing:border-box; width:100%%; padding:0 10px; letter-spacing:0.1px;color:#121212;text-indent:2em;font-size:%@px;line-height:150%%; overflow: hidden;word-wrap:break-word; word-break: bre-all;height:auto;zoom:1;text-align:justify;text-justify:inter-ideograph;padding-left:2px;font-family:System;}#news_title {font-weight:bold;text-align:left;-webkit-box-sizing:border-box;box-sizing:border-box; width:100%%;letter-spacing:0.1px;color:#121212;font-size:20px;line-height:120%%; overflow: hidden;word-wrap:break-word; word-break: bre-all;height:auto;zoom:1;text-align:justify;text-justify:inter-ideograph;font-family:System;padding-right:6px};</style>",newscontent_fontsize];
            
            result =[result stringByReplacingOccurrencesOfString:@"<![CDATA[" withString:@""];
            result= [result stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
            
            NSString *title         = [EpointStringUtil getXMLFormatedAttribute:result :@"title"];
            NSString *infodate      = [EpointStringUtil getXMLFormatedAttribute:result :@"infodate"];
            NSString *infocontent   = [EpointStringUtil getXMLFormatedAttribute:result :@"infocontent"];
            
            NSString *disContent = [NSString stringWithFormat:@"%@<div id='news_title'>%@</div><div id='news_outline'>%@ %@</div>%@<br />",mycss,title,@"",[self dealDate:infodate],infocontent];
            
            [mWebView loadHTMLString:disContent baseURL:nil];
        }
        

    }
}

#pragma 处理日期类型
-(NSString *)dealDate:(NSString *)_infoDate{
    if([_infoDate rangeOfString:@"/"].location!=NSNotFound || [_infoDate rangeOfString:@"-"].location!=NSNotFound){
        NSArray *str = [_infoDate componentsSeparatedByString:@" "];
        NSString *temp = [NSString stringWithFormat:@"%@: %@",@"发布时间",[str objectAtIndex:0]];
        return  temp;
    }else{
        return [NSString stringWithFormat:@"%@: %@",@"发布时间",_infoDate];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellid = @"WSBSItemCell";
    WSBSItemCell *cell = (WSBSItemCell *)[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"WSBSItemCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    WSBSInfoListModel *info = [infoListArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = info.Title;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [infoListArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [_btnList showAndHideButtonList];
    
    WSBSInfoListModel *info = [infoListArray objectAtIndex:indexPath.row];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:info.InfoID forKey:@"infoid"];
    WMH_WSBS_InfoDetail_ViewController *view = [[WMH_WSBS_InfoDetail_ViewController alloc]init];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}


@end
